<?php
/**
 * Webtaculous_Mail_Auto
 *
 * @package default
 * @author Colin Burn-Murdoch
 **/
class Webtaculous_Mail_Auto {
	
	private $guid;
	private $record;
	
	private $subject, $from, $from_name, $to, $to_name, $values = array();
	
	public function __construct($guid) {
		$this->guid = $guid;
		
		$this->record = Store::getInstance()->db->fetchRow("SELECT * FROM cms_email_templates WHERE guid=?", $this->guid);
		
	}
	
	public function to($to) {
		$this->to = $to;
	}
	public function to_name($to_name) {
		$this->to_name = $to_name;
	}
	public function from($from) {
		$this->from = $from;
	}
	public function from_name($from_name) {
		$this->from_name = $from_name;
	}
	public function subject($subject) {
		$this->subject = $subject;
	}
	
	public function value($key, $value) {
		$this->values[$key] = $value;
	}
	
	public function send() {
		
		if (!$this->record) $this->record = $this->createRecord();
		
		if (!$this->hasContent()) return false;
		
		foreach(array('to', 'to_name', 'from', 'from_name', 'subject') as $field) {
			if ($this->$field) $this->record[$field] = $this->$field;
			else $this->record[$field] = $this->record['default_'.$field];
		}
		
		$mail = new Webtaculous_Mail('UTF-8', Store::getInstance()->db, Store::getInstance()->site['id']);
		
		$smarty = clone(Store::getInstance()->smarty);
		
		foreach ($this->values as $key => $value) {
			$smarty->assign($key, $value);
		}
		
		$mail->setBodyText($smarty->fetch('string:' . $this->record['body_text']));
		if ($this->record['body_html']) $mail->setBodyHtml($smarty->fetch('string:' . $this->record['body_html']));
		
		$mail->setFrom($this->record['from'], $this->record['from_name']);
		$mail->addTo($this->record['to'], $this->record['to_name']);
		$mail->setSubject($this->record['subject']);
		
		return $mail->send();
		
	}
	
	private function hasContent() {
		return (bool)($this->record['body_text'] or $this->record['body_html']);
	}
	
	private function createRecord() {
		
		$body_text = '';
		$body_html =  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . "\n";
		$body_html .= '	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";
		$body_html .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' . "\n";
		$body_html .= '<head>' . "\n";
		$body_html .= '	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' . "\n";
		$body_html .= '	<title>' . ($this->subject ? $this->subject : 'Email') . '</title>' . "\n";
		$body_html .= '</head>' . "\n";
		$body_html .= '<body>' . "\n";
		$body_html .= "\n";
		$body_html .= '<table>' . "\n";
		foreach ($this->values as $key => $value) {
			$body_text .= "{$key}: {\${$key}}\n";
			$body_html .= "\t<tr>\n";
			$body_html .= "\t\t<th>{$key}:</th>\n";
			$body_html .= "\t\t<td>{\${$key}}</td>\n";
			$body_html .= "\t</tr>\n";
		}
		$body_html .= '</table>' . "\n";
		$body_html .= "\n";
		$body_html .= '</body>' . "\n";
		$body_html .= '</html>' . "\n";
		
		$data = array(
			'site_id'           => Store::getInstance()->site['id'],
			'guid'              => $this->guid,
			'body_text'         => $body_text,
			'body_html'         => $body_html
		);
		
		if ($this->to) $data['default_to']               = $this->to;
		if ($this->to_name) $data['default_to_name']     = $this->to_name;
		if ($this->from) $data['default_from']           = $this->from;
		if ($this->from_name) $data['default_from_name'] = $this->from_name;
		if ($this->subject) $data['default_subject']     = $this->subject;
		
		Store::getInstance()->db->insert('cms_email_templates', $data);
		
		return Store::getInstance()->db->fetchRow("SELECT * FROM cms_email_templates WHERE guid=?", Store::getInstance()->db->lastInsertId());
		
	}
	
	

	
}


